// service/issueYhq/issueYhq.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    safeBottom: `30px`,
    type:2,
    effective_day: '',
    achieve: '',
    reduce: '',
    starttime: '',
    endtime: '',
    goods_id: '',
    name:'',
    show: false,
    date: '',
    id: '',
    token: ''
  },
  getInfo(){
    if(this.data.id)
    http.post('shop/editcoupon',{
      id: this.data.id,
      types: 'info'
    }).then(res => {
      this.setData({
        type: res.data.type,
        effective_day: res.data.effective_day,
        achieve: res.data.achieve,
        reduce: res.data.reduce,
        starttime: res.data.starttime,
        endtime: res.data.endtime,
        goods_id: res.data.goods_id,
        name: res.data.name,
        date: `${this.formatDate(res.data.starttime*1000)} - ${this.formatDate(res.data.endtime*1000)}`
      })
    })
  },
  save(){
    if (this.data.achieve === '') return util.toast('请输入满减金额')
    if (this.data.reduce === '') return util.toast('请输入满减金额')
    if(this.data.type == 3 && this.data.goods_id === '') return util.toast('请选择项目')
      if (this.data.starttime === '') return util.toast('请选择展示时间')
      if (this.data.endtime === '') return util.toast('请选择展示时间')
      if (this.data.effective_day === '') return util.toast('请输入有效期')
      let data = {
        type: this.data.type,
        achieve: this.data.achieve,
        reduce: this.data.reduce,
        goods_id: this.data.goods_id,
        starttime: this.data.starttime,
        endtime: this.data.endtime,
        effective_day: this.data.effective_day,
      }
      if(this.data.id){
        data['id'] = this.data.id
        data['types'] = 'edit'
      }else{
        data['types'] = 'add'
      }
      http.post('shop/editcoupon',data).then(res => {
        util.toast(res.msg)
        setTimeout(()=>{
          util.back()
        },1000)
      })
  },
  onClose(){
    this.setData({
      show: false
    })
  },
  showCalendar(){
    this.setData({
      show: true
    })
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      show: false,
      starttime: new Date(start).getTime()/1000,
      endtime: new Date(end).getTime()/1000,
      date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
    });
  },

  selfList(){
    util.skip('/service/selfserveList/selfserveList?goods_id='+this.data.goods_id+'&name='+this.data.name,{
      selectSelfSevel: (data)=>{
        this.setData({
          name: data.name,
          goods_id: data.goods_id
        })
      }
    })
  },
  setAchieve(e){
    this.setData({
      achieve: e.detail.value
    })
  },
  setReduce(e){
    this.setData({
      reduce: e.detail.value
    })
  },
  setEffective_day(e){
    this.setData({
      effective_day: e.detail.value
    })
  },
  setType(e){
    this.setData({
      type: Number(e.currentTarget.dataset.type)
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const high = app.globalData.safeBottom
    this.setData({
      safeBottom: `${high}px`,
      id: options.id || '',
      token: wx.getStorageSync('token')
    })
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})